//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

package com.example.sens.utils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.Instant;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.Period;
import java.time.ZoneId;
import java.util.Calendar;
import java.util.Date;

public class DateUtils {
    public static final int HOUR = 1;
    public static final int Day = 2;
    public static final int MONTH = 3;
    public static final int YEAR = 4;

    public DateUtils() {
    }

    public static BaseDateDiffResult DateDiff(Date timeStart, Date timeEnd) {
        BaseDateDiffResult baseDateDiffResult = new BaseDateDiffResult();
        if (timeStart != null && timeEnd != null && !timeStart.after(timeEnd)) {
            LocalDate startTime = UDateToLocalDate(timeStart);
            LocalDate endTime = UDateToLocalDate(timeEnd);
            Period p = Period.between(startTime, endTime);
            if (p.getYears() != 0) {
                baseDateDiffResult.setDiffNum(p.getYears());
                baseDateDiffResult.setDiffUnit(Integer.valueOf(4));
            } else if (p.getMonths() != 0) {
                baseDateDiffResult.setDiffNum(p.getMonths());
                baseDateDiffResult.setDiffUnit(Integer.valueOf(3));
            } else if (p.getDays() != 0) {
                baseDateDiffResult.setDiffNum(p.getDays());
                baseDateDiffResult.setDiffUnit(Integer.valueOf(2));
            } else {
                long nd = 86400000L;
                long nh = 3600000L;
                long diff = timeEnd.getTime() - timeStart.getTime();
                Integer hour = (int)(diff % nd / nh);
                baseDateDiffResult.setDiffNum(hour);
                baseDateDiffResult.setDiffUnit(Integer.valueOf(1));
            }

            return baseDateDiffResult;
        } else {
            return baseDateDiffResult;
        }
    }

    public static LocalDate UDateToLocalDate(Date date) {
        if (date == null) {
            return null;
        } else {
            Instant instant = date.toInstant();
            ZoneId zone = ZoneId.systemDefault();
            LocalDateTime localDateTime = LocalDateTime.ofInstant(instant, zone);
            LocalDate localDate = localDateTime.toLocalDate();
            return localDate;
        }
    }

    public static String DateFormatToString(Date time) {
        SimpleDateFormat dateFormate = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return dateFormate.format(time);
    }

    public static String DateFormatToString(Date time, String fmt) {
        SimpleDateFormat dateFormate = new SimpleDateFormat(fmt);
        return dateFormate.format(time);
    }

    public static Date DateFormatToDate(String time) {
        Date date = null;

        try {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            date = sdf.parse(time);
        } catch (ParseException var3) {
            ;
        }

        return date;
    }

    public static Date DateFormatToDate(String time, String fmt) {
        Date date = null;

        try {
            SimpleDateFormat sdf = new SimpleDateFormat(fmt);
            date = sdf.parse(time);
        } catch (ParseException var4) {
            ;
        }

        return date;
    }

    public static Date addDay(Date time, int amount) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(time);
        cal.add(5, amount);
        return cal.getTime();
    }

    public static Date beginOfDay(Date time) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(time);
        calendar.set(11, 0);
        calendar.set(12, 0);
        calendar.set(13, 0);
        return calendar.getTime();
    }

    public static Date endOfDay(Date time) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(time);
        calendar.set(11, 23);
        calendar.set(12, 59);
        calendar.set(13, 59);
        return calendar.getTime();
    }

    public static Date addWorkingDays(Date startDate, int days) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(startDate);
        int i = 1;

        while(i <= days) {
            calendar.add(7, 1);
            int weekDay = calendar.get(7);
            if (weekDay != 7 && weekDay != 1) {
                ++i;
            }
        }

        return calendar.getTime();
    }

    public static int differentDaysByMillisecond(Date date1, Date date2) {
        long time = date2.getTime() - date1.getTime();
        int days = (int)(time / 86400000L);
        if (time < 0L) {
            --days;
        }

        return days;
    }

    public static int differentWorkingDays(Date date2, Date date1) {
        Date beginDate = date2;
        Date endDate = date1;
        if (date1.compareTo(date2) < 0) {
            beginDate = date1;
            endDate = date2;
        } else if (date1.compareTo(date2) == 0) {
            return 0;
        }

        int day = 0;
        Calendar calendar = Calendar.getInstance();
        int weekDay = 0;

        while(beginDate.compareTo(endDate) < 0 || weekDay == 7 || weekDay == 1) {
            calendar.setTime(beginDate);
            calendar.add(7, 1);
            beginDate = calendar.getTime();
            weekDay = calendar.get(7);
            if (weekDay != 7 && weekDay != 1) {
                ++day;
            }
        }

        if (day > 0 && date1.compareTo(date2) > 0) {
            --day;
        } else if (date1.compareTo(date2) < 0) {
            day = -day;
        }

        return day;
    }
}
